#define _CRT_SECURE_NO_WARNINGS 1
#include"queue.h"

void QueueInit(Queue* pq)
{
	assert(pq);
	pq->head = NULL;
	pq->tail = NULL;
}

void QueueDestory(Queue* pq)
{
	assert(pq);
	QNode* cur = pq->head;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->head = pq->tail = NULL;
}


void QueuePush(Queue* pq, QuDateType x)
{
	assert(pq);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	assert(newnode);
	newnode->date = x;
	newnode->next = NULL;
	if (pq->tail == NULL)
	{
		assert(pq->head == NULL);
		pq->head = pq->tail = newnode;
	}
	else
	{
		 pq->tail->next = newnode;
		 pq->tail = newnode;
	}
}

void QueuePop(Queue* pq)
{
	assert(pq);
	if ((pq->head == pq->tail)&&(pq->head == NULL))
	{ 
		return;
	}
	else if (pq->head == pq->tail&&pq->head!=NULL)
	{
		free(pq->head);
		pq->head = pq->tail = NULL;
	}
	else
	{
		QNode* next= pq->head->next;
		free(pq->head);
		pq->head = next;
	}
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->head == NULL && pq->tail == NULL;
}

size_t QueueSize(Queue* pq)
{
	assert(pq);
	int n = 0;
	QNode* cur = pq->head;
	while (cur)
	{
		n++;
		cur = cur->next;
	}
	return n;
}

QuDateType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->head);
	return pq->head->date;
}

QuDateType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->tail);
	return pq->tail->date;
}

//void print(Queue* pq)
//{
//	assert(pq);
//	QNode* cur = pq->head;
//	while (cur)
//	{
//		printf("%d->", cur->date);
//		cur = cur->next;
//	}
//	printf("\n");
//}